package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.sys.SysUserDTO;
import com.atguigu.spzx.model.entity.sys.SysRole;
import com.atguigu.spzx.model.entity.sys.SysUser;
import com.atguigu.spzx.model.vo.common.R;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Tag(name = "用户管理模块")
@RequestMapping("/admin/system/user")
public class SysUserController {
    @Resource
    SysUserService sysUserService;
    @Operation(summary = "条件查询用户分页列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum")Integer pageNum ,
                  @PathVariable("pageSize")Integer pageSize,
                  // Get请求没有请求体， @RequestBody 表示从请求体中获取json转为对象
                  //不使用注解表示POJO入参，获取请求参数设置给对象 url?keyword=xx&begin=xxx&end=xxx
                  SysUserDTO sysUserDTO){
        PageInfo<SysUser> page =  sysUserService.list(pageNum,pageSize , sysUserDTO);
        return R.ok("page" , page);
    }

    @Operation(summary = "新增用户")
    @PostMapping("save")
    public R save(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "删除指定用户")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id){
        sysUserService.deleteByIds(Arrays.asList(id));
        return R.ok();
    }
    @Operation(summary = "查询指定用户")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Long id){
        SysUser sysUser = sysUserService.getById(id);
        return R.ok("item",sysUser);
    }
    @Operation(summary = "更新指定用户")
    @PutMapping("update")
    public R update(@RequestBody SysUser sysUser){
        sysUserService.update(sysUser);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "查询所有用户")
    @GetMapping("list")
    public R list(){
        List<SysUser> sysUsers =  sysUserService.list();
        return R.ok("items" , sysUsers);
    }
    @Operation(summary = "给用户分配角色")
    @PutMapping("assignRoles2User/{id}")
    // url?roleids=1,2,3  或者 url?roleids=1&roleids=2&roleids=3    @RequestParam("roleids") List<Long> roleids
    //请求体： [1,2,3]   @RequestBody List<Long> list
    //     [{},{},{}]   @RequestBody List<T> list
    public R assignRoles2User(@PathVariable("id")Long id ,
                              @RequestParam("roleIds")List<Long> roledIs ){
        sysUserService.assignRoles2User(id , roledIs);
        return R.ok();
    }

}
